import os
from conf import settings
from db import creat_path, read_data


def name_list():
    user_list = os.listdir(path=settings.USER_INFO_DIR)
    list_name = []
    for i in user_list:
        name = i[:-10]
        list_name.append(name)
    return list_name


def index_name_dict():
    list_name = name_list()
    name_dict = dict(enumerate(list_name, 1))
    return name_dict


def show_user_info(username):
    path = creat_path(tag='info', path=f"{username}_info.json")
    flag, user_data = read_data(tag='info', path=path)
    if not flag:
        return flag, user_data
    msg = f"""
    用户{username} 信息如下:
    是否是管理员 :{user_data[username]['is_admin']}
    银行卡号 :{user_data[username]['bank_id']}
    余额 :{user_data[username]['balance']}
    购物车 :{user_data[username]['shop_cart']}
    """
    return True, msg


def get_check_username():
    name_dict = index_name_dict()
    for i in name_dict.items():
        print(i)
    check_choice = input("请输入要用户编号:").strip()
    if not check_choice.isdigit():
        return False, "请输入数字编号！"
    check_choice = int(check_choice)
    if check_choice not in name_dict.keys():
        return False, "输入有误！"
    check_name = name_dict[check_choice]
    return True, check_name
